package com.sundablog.controller.systemsetup.permissionsmanage.role;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sundablog.pojo.AdminRole;
import com.sundablog.pojo.AdminUser;
import com.sundablog.pojo.AdminUserRole;
import com.sundablog.result.BaseResult;
import com.sundablog.result.LayUiResult;
import com.sundablog.service.systemsetup.permissionsManage.role.RoleService;
import com.sundablog.utlis.RedisUtil;

/**
 * 角色控制器类
 * @ClassName:  RoleController   
 * @Description:角色控制器类  
 * @author: 哒哒 
 * @date:   2018年2月11日 下午10:42:19   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("/systemsetup/permissionsmanage/role/")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	/**
	 * 角色界面显示
	 * @Title: showIndex   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_index")
	@RequestMapping("/index")
	public String showIndex() {
		return "/systemsetup/permissionsmanage/role/index";
	}
	
	/**
	 * 分页查询
	 * @Title: findPagingQuery   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param page
	 * @param: @param limit
	 * @param: @return      
	 * @return: LayUiResult      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_findPagingQuery")
	@RequestMapping("/findPagingQuery")
	@ResponseBody
	public LayUiResult findPagingQuery(@RequestParam(value="page", defaultValue="1") int page,@RequestParam(value="limit", defaultValue="10") int limit) {
		LayUiResult result = roleService.findPagingQuery(page, limit);
		return result;
	}
	
	/**
	 * 添加角色界面显示
	 * @Title: addIndex   
	 * @Description: 添加角色界面显示   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_addIndex")
	@RequestMapping("/addIndex")
	public String addIndex() {
		return "/systemsetup/permissionsmanage/role/add";
	}
	
	/**
	 * 添加一条角色
	 * @Title: roleAdd   
	 * @Description: 添加一条角色   
	 * @param: @param adminRole
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_roleAdd")
	@RequestMapping("/roleAdd")
	@ResponseBody
	public BaseResult roleAdd(AdminRole adminRole,String zTreeResults) {
		int i = roleService.addRole(adminRole,zTreeResults);
		if (i > 0) {
			return BaseResult.build(200, BaseResult.addSuccess);
		} else {
			return BaseResult.build(209, BaseResult.addFail);
		}
	}
	
	/**
	 * 删除角色
	 * @Title: delectRole   
	 * @Description: 删除角色   
	 * @param: @param id
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_delectRole")
	@RequestMapping("/delectRole")
	@ResponseBody
	public BaseResult delectRole(int id) {
		int i = roleService.delectAdminRoleById(id);
		if (i > 0) {
			return BaseResult.build(200, BaseResult.delectSuccess);
		} else {
			return BaseResult.build(209, BaseResult.delectFail);
		}
	}
	
	/**
	 * 修改界面显示
	 * @Title: editIndex   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_editIndex")
	@RequestMapping("/editIndex")
	public String editIndex() {
		return "/systemsetup/permissionsmanage/role/edit";
	}
	
	/**
	 * 查询一条角色信息
	 * @Title: selectAdminRoleById   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param id
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_selectAdminRoleById")
	@RequestMapping("/selectAdminRoleById")
	@ResponseBody
	public BaseResult selectAdminRoleById(int id) {
		AdminRole adminRole = roleService.selectAdminRoleById(id);
		if (adminRole != null) {
			return BaseResult.build(200, "", adminRole);
		} else {
			return BaseResult.build(209, BaseResult.selectFail);
		}
	}
	
	/**
	 * 修改一条角色信息
	 * @Title: updataAdminRole   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param adminRole
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_updataAdminRole")
	@RequestMapping("/updataAdminRole")
	@ResponseBody
	public BaseResult updataAdminRole(AdminRole adminRole,String zTreeResults) {
		int i = roleService.updataAdminRole(adminRole,zTreeResults);
		if (i > 0) {
			//更新缓存
			
			List<AdminUserRole> list = roleService.selectByRoleId(adminRole.getRoleId());
			for (AdminUserRole adminUserRole : list) {
				RedisUtil.set("backend_reaim_"+adminUserRole.getUserId(),"");
				RedisUtil.set("backend_Menu_"+adminUserRole.getUserId(),"");
			}
			
			return BaseResult.build(200, BaseResult.updataSuccess);
		} else {
			return BaseResult.build(209, BaseResult.updataFail);
		}
	}
	
	/**
	 * 查看界面显示
	 * @Title: detailIndex   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_detailIndex")
	@RequestMapping("/detailIndex")
	public String detailIndex() {
		return "/systemsetup/permissionsmanage/role/detail";
	}
	
	
	/**
	 * 查询全部角色
	 * @Title: selectRole   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequiresPermissions("systemsetup_permissionsmanage_role_selectRole")
	@RequestMapping("/selectRole")
	@ResponseBody
	public BaseResult selectRole() {
		List<AdminRole> list = roleService.selectRole();
		return BaseResult.build(200, "", list);
	}
	
	
}
